package cn.wolfocde.wolf2w.controller;

import cn.wolfocde.wolf2w.domain.Destination;
import cn.wolfocde.wolf2w.domain.Region;
import cn.wolfocde.wolf2w.query.QueryObject;
import cn.wolfocde.wolf2w.repository.RegionRepository;
import cn.wolfocde.wolf2w.service.IRegionService;
import cn.wolfocde.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

   @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {
       //查询分页排序后的结果集合
       model.addAttribute("page", regionService.query(qo));
        return "region/list";
    }

   @RequestMapping("/saveOrUpdate")
   @ResponseBody
    public Object saveOrUpdate(Region region) {
       if (StringUtils.hasLength(region.getId())) {
           regionService.update(region);
       }else{
           regionService.save(region);
       }
        return JsonResult.success();
    }
    //根据区域id查询区域下的所有目的地对象集合
   @GetMapping("/getDestByRegionId")
   @ResponseBody
    public Object getDestByRegionId(String rid) {
       List<Destination> destinations= regionService.getDestByRegionId(rid);
        return destinations;
    }
    //查询编辑/添加页面关联下拉框的所有目的地集合
   @GetMapping("/getListDest")
   @ResponseBody
    public Object getListDest() {
       List<Destination> destinations= regionService.getDestByDeep();
        return destinations;
    }
}
